package com.shopping.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shopping.bean.Goods;
import com.shopping.bean.User;
import com.shopping.service.UserHobbyService;
import com.shopping.service.impl.UserHobbyServiceImpl;

/**
 * @author zjh
 *
 */
@WebServlet("/collect.do")
public class CollectServlet extends HttpServlet {
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		User user = (User) req.getSession().getAttribute("loginUser");
		UserHobbyService uhs = new UserHobbyServiceImpl();
		List<Integer> goodsIds = uhs.getGoodsId(user.getUserId());
		List<Goods> goods = new ArrayList<Goods>();
		for (Integer goodsId : goodsIds) {
			List<Goods> list = uhs.getGoods(goodsId);
			if(list != null) {
				for (Goods good : list) {
					goods.add(good);
				}
			}
		}
		req.setAttribute("hobbyGoods", goods);
		req.getRequestDispatcher("collect.jsp").forward(req, resp);
	}
	
}
